package vnreal.ZNEW.Algorithms;

import vnreal.ZNEW.GraphTest.CostHelper;
import vnreal.ZNEW.GraphTest.Link;
import vnreal.ZNEW.GraphTest.Node;
import vnreal.ZNEW.GraphTest.Path;

public class EA4 extends EnergyAlgorithm {

	private final static EA4 ea4;

	static {
		ea4 = new EA4();
	}

	private EA4() {
		super();
	}

	public static EA4 getAlgorithm() {

		return ea4;
	}

	@Override
	protected double calculateCost(Node node, Link lnk, int i, Path p) {
		double currentCost = 0;
		double bw = Double.parseDouble(lnk.bandwith);
		int ports = 0;
		int lineCards = node.lineCards;
		ports = getNeededPorts(bw); // potrzebne + resztka
		if (i > 0 && i < p.allPaths.size() - 1) {
			ports *= 2;
		}
		if (node.isOn) {
			lineCards = getNeededLineCards(node.ports + ports) - lineCards;
		} else {
			lineCards = getNeededLineCards(ports);
			currentCost += Double.valueOf(node.energy) * CostHelper.CostOfNodeOn;
		}
		currentCost += CostHelper.turnOnLineCard * lineCards; // turnON next
																// linecards
		currentCost += CostHelper.turnOnPort * ports; // turnON ports
		return currentCost;
	}

	private int getNeededPorts(double bw) {
		if (bw % 1000 == 0) {
			return (int) (bw / 1000);
		} else {
			return (int) (bw / 1000) + 1;
		}
	}

	private int getNeededLineCards(int ports) {
		if (ports % 48 == 0) {
			return (int) (ports / 48);
		} else {
			return (int) (ports / 48) + 1;
		}
	}
}